package esa.isoc.statistics;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.awt.RenderingHints;
import java.util.List;
import java.util.Map.Entry;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.FastScatterPlot;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PlotExample extends ApplicationFrame {

   

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** A constant for the number of items in the sample dataset. */
    private static final int REVOLUTIONS = 1129;

    /** The data. */
    private float[][] data = new float[2][REVOLUTIONS];

    /**
     * Creates a new fast scatter plot demo.
     *
     * @param title  the frame title.
     */
    public PlotExample(final String title) {

        super(title);
        StatisticsRow row = StatisticsRow.TOTAL_OPEN_TIME;
        populateData(row);
        final NumberAxis domainAxis = new NumberAxis("Revolution");
        
        domainAxis.setAutoRangeIncludesZero(false);
        final NumberAxis rangeAxis = new NumberAxis(row.toString());
        
        //rangeAxis.setAutoRangeIncludesZero(false);
        rangeAxis.setAutoRange(true);
        final FastScatterPlot plot = new FastScatterPlot(this.data, domainAxis, rangeAxis);
        
        final JFreeChart chart = new JFreeChart(row.description, plot);
//        chart.setLegend(null);

        // force aliasing of the rendered content..
        chart.getRenderingHints().put
            (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        final ChartPanel panel = new ChartPanel(chart, true);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        
        panel.setMinimumDrawHeight(100);
        panel.setMaximumDrawHeight(20000);
        panel.setMinimumDrawWidth(200);
        panel.setMaximumDrawWidth(20000);
        
        setContentPane(panel);

    }

   
    
    private void populateData(StatisticsRow row) {
    FileManager fManager = new FileManager();
	List<StatisticFile> statisticsFiles = fManager.getStatisticsFiles();
	int count = 11;
	int discardedValues = 0;
	for (StatisticFile file : statisticsFiles) {
			file.readFileData();
			
	            
	        this.data[0][count] = file.revolutionNumber;
	            	
	        if (file.mapEntries.containsKey(row.description)) {
				float value = (float)file.mapEntries.get(row.description).getFraction();
				if (value>0) {
					this.data[1][count] = value;
					
				} 
			/*	else {
					this.data[1][count] = 0;
					
			        System.out.println("discarded " + this.data[0][count] +"= "+ value);
					discardedValues++;
				}*/
			}
 	        
	        count++;   

		
		 
	}
    }
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final PlotExample demo = new PlotExample("Scatter Plot");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}